home *** CD-ROM | disk | FTP | other *** search
- /*============================================================
- FailureHander.h
-
- greggor@apple.com
- ============================================================*/
- #ifndef __FAILUREHANDLER__
- #define __FAILUREHANDLER__
-
- #ifndef __RESOURCES__
- #include <Resources.h>
- #endif
-
- #ifndef __MEMORY__
- #include <Memory.h>
- #endif
-
- #include <setjmp.h>
-
- #define ePointerNil -110
- #define eGeneralErr -1
- #define eAssertionFailure -30000
-
- #define kMagicFailID 'fail'
-
- #define kInExceptionHandler 0x001
-
- /*
- // Data types:
- */
- struct FailStack
- {
- struct FailStack* next;
- OSErr error;
- jmp_buf env;
- long magicFailID;
- long flags;
- };
-
- typedef struct FailStack FailStack;
-
- typedef void (*NotifyFailureProc)(void);
-
- /*
- // Globals:
- */
- extern FailStack* gFailStack;
-
- /*
- // Prototypes
- */
- void SetupTry(FailStack* failInfo);
- void PopFailureStack(void);
- void Failure( OSErr theErr );
- void FailOSErr( OSErr theErr );
- OSErr TheFailError(void);
- void FailAgain(void);
-
- void SetExceptionNotifyProc(NotifyFailureProc notifyProc);
- Boolean ExceptionNotifyProcInstalled(void);
-
- void MakeVariableNoRegister( void* foo );
-
- /*
- // TRY / EXCEPT / ENDTRY macros
- */
- #define TRY do { FailStack failInfo; SetupTry(&failInfo); if( setjmp(failInfo.env) == 0 )
- #define EXCEPT else
- #define ENDTRY PopFailureStack(); } while(false);
-
- /*
- // This macro references the address of the specified local
- // variable; this will prevent the compiler from using the
- // variable as a register variable.
- */
- #define NOREGISTER(x) ( MakeVariableNoRegister( (void*)(&(x)) ) )
-
- /*
- // Convenience macros
- */
- #define FailResError() FailOSErr( ResError() )
- #define FailMemError() FailOSErr( MemError() )
- #define FailThreadError() FailOSErr( ThreadError() )
- #define FailNil( ptr ) if( ptr == nil ) FailOSErr( ePointerNil )
- #define Fail() FailOSErr( eGeneralErr )
- #define FailResErrorOrNil(ptr) do { FailResError(); FailNil(ptr); } while(false)
- #define FailMemErrorOrNil(ptr) do { FailMemError(); FailNil(ptr); } while(false)
-
- /*
- // Assertion macros
- */
- #define Assert(truth) if( !(truth) ) FailOSErr( eAssertionFailure )
- #define AssertPtrValid(p) if( (p == nil) || (((long)(p) & 1) != 0) ) FailOSErr( eAssertionFailure )
- #define AssertHandleValid(h) AssertPtrValid(h); AssertPtrValid(*h)
-
- #endif
-